﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sausage.Web.Code;
using Sausage.Common;
using Sausage.Web.Domain;
using Sausage.Web.Models;
using Sausage.Web.DataModel;

namespace Sausage.Web.Controllers
{
    /// <summary>
    /// 数据字典控制器
    /// </summary>
    /// <remarks></remarks>
    public class DictionaryController : Controller
    {
        /// <summary>
        /// 默认动作。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 查询工程项目信息列表
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>查询的工程项目信息列表</returns>
        /// <remarks></remarks>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Select(JosnPagingQueryModel query)
        {
            var result = DictionaryHelper.QueryDictionaryItem(query);
            var list = result.Data.Select(dictionary => new DictionaryItemListRow()
                                                            {
                                                                DictionayID = dictionary.DictionayID,
                                                                ItemID = dictionary.ItemID,
                                                                ItemName = dictionary.ItemName,
                                                                ItemPinYin = dictionary.ItemPinYin,
                                                                ItemValue = dictionary.ItemValue,
                                                                TheOrder = dictionary.TheOrder
                                                            }).ToList<DictionaryItemListRow>();
            //返回结果
            return Json(
                new JsonResultViewModel { success = true, total = result.Total, message = "字典数据加载成功！", data = list },
                JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 删除工程信息
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Delete(DictionaryItemListRow data)
        {
            var dictionaryItem = DictionaryHelper.GetDictionaryItemByID(data.ItemID);

            if (dictionaryItem == null)
                return Json(new JsonResultViewModel { success = false, message = "没有此字典数据！", data = null },
                JsonRequestBehavior.AllowGet);

            if (DictionaryHelper.DeleteDictionaryItem(data.ItemID) == DeleteResult.UsingError)
                return Json(new JsonResultViewModel { success = false, message = "字典数据删除失败，其它数据正在使用此字典数据！", data = null },
                            JsonRequestBehavior.AllowGet);

            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "字典数据删除成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 创建线路
        /// </summary>
        /// <returns>要创建的线路信息。</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Create(DictionaryItemListRow data)
        {
            var dictionaryItem = DictionaryHelper.GetDictionaryItemByName(data.ItemName);
            if (dictionaryItem != null &&
                dictionaryItem.DictionayID == data.DictionayID)

                return Json(new JsonResultViewModel { success = false, message = "已有同名字典数据在同字典分类，请更换字典数据名称！", data = null },
                            JsonRequestBehavior.AllowGet);

            DictionaryHelper.CreateDictionaryItem(data);

            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "字典数据添加成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获得指定线路的详细信息。
        /// </summary>
        /// <param name="query">账户编号。</param>
        /// <returns>指定账户的详细信息。</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Details(Guid id)
        {
            var dictionaryItem = DictionaryHelper.GetDictionaryItemByID(id);
            if (dictionaryItem == null)
                return Json(new JsonResultViewModel { success = false, message = "查找不到此字典数据！", data = null },
                            JsonRequestBehavior.AllowGet);


            return Json(new JsonResultViewModel
                            {
                                data = new DictionaryItemListRow()
                                           {
                                               DictionayID = dictionaryItem.DictionayID,
                                               ItemID = dictionaryItem.ItemID,
                                               ItemName = dictionaryItem.ItemName,
                                               ItemPinYin = dictionaryItem.ItemPinYin,
                                               ItemValue = dictionaryItem.ItemValue,
                                               TheOrder = dictionaryItem.TheOrder
                                           },
                                success = true,
                                message = "字典数据添加成功！"
                            }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新路线。
        /// </summary>
        /// <param name="data">要更新的路线信息。</param>
        /// <returns></returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Update(DictionaryItemListRow data)
        {
            var dictionaryItem = DictionaryHelper.GetDictionaryItemByName(data.ItemName);
            if (dictionaryItem != null &&
                dictionaryItem.ItemID != data.ItemID &&
                data.DictionayID == dictionaryItem.DictionayID)
                return Json(new JsonResultViewModel { success = false, message = "已有同名字典数据在同字典分类，请更换字典数据名称！", data = null },
                            JsonRequestBehavior.AllowGet);

            DictionaryHelper.UpdateDictionaryItem(data);

            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "更新字典数据成功！" }, JsonRequestBehavior.AllowGet);
        }
    }
}
